package com.atguigu.tingshu.order.strategy.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.order.strategy.OrderTradeStrategy;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.Date;

@Service
@Slf4j
public class AlbumOrderTradeStrategy implements OrderTradeStrategy {

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;
    @Autowired
    private UserInfoFeignClient userInfoFeignClient;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public OrderInfoVo trade(TradeVo tradeVo) {
        log.info("AlbumOrderTradeStrategy....trade方法执行了");
        // 构建OrderInfoVo对象
        OrderInfoVo orderInfoVo = new OrderInfoVo();

        // 根据专辑的id查询专辑数据
        Long itemId = tradeVo.getItemId();
        Result<AlbumInfo> albumInfoResult  = albumInfoFeignClient.getAlbumInfo(itemId);
        AlbumInfo albumInfo = albumInfoResult .getData();

        // 获取当前登录用户的id
        Long userId = AuthContextHolder.getUserId();
        Result<UserInfoVo> userInfoVoResult = userInfoFeignClient.findUserInfoVoByUserId(userId);
        UserInfoVo userInfoVo = userInfoVoResult.getData();

        //构建金额数据
        BigDecimal originalAmount  = albumInfo.getPrice();
        orderInfoVo.setOriginalAmount(originalAmount);

        if(userInfoVo.getIsVip().equals("1") && new Date().before(userInfoVo.getVipExpireTime())) {     // 是vip用户并且vip没有过期

        // 按照vip的折扣进行减免金额
            BigDecimal vipDiscount = albumInfo.getVipDiscount();
            if (vipDiscount.intValue()!= 1){
                BigDecimal orderAmount = originalAmount.multiply(vipDiscount)
                        .multiply(new BigDecimal("10"))
                        .divide(new BigDecimal("10"), 2, RoundingMode.HALF_UP);
                orderInfoVo.setOrderAmount(orderAmount);
            }else {
                orderInfoVo.setOrderAmount(originalAmount);
            }
        }else {
            // 不是vip用户或者vip已经过期
            BigDecimal discount = albumInfo.getDiscount();
            if(discount.intValue() != -1) {
                BigDecimal orderAmount = originalAmount.multiply(discount)
                        .multiply(new BigDecimal("10"))
                        .divide(new BigDecimal("10"), 2, RoundingMode.HALF_UP);
                orderInfoVo.setOrderAmount(orderAmount);
            }else {
                orderInfoVo.setOrderAmount(originalAmount);
            }
        }
        orderInfoVo.setDerateAmount(originalAmount.subtract(orderInfoVo.getOrderAmount()));


        // 订单明细数据
        OrderDetailVo orderDetailVo = new OrderDetailVo() ;
        orderDetailVo.setItemId(itemId);
        orderDetailVo.setItemName(albumInfo.getAlbumTitle());
        orderDetailVo.setItemUrl(albumInfo.getCoverUrl());
        orderDetailVo.setItemPrice(orderInfoVo.getOrderAmount());
        orderInfoVo.setOrderDetailVoList(Arrays.asList(orderDetailVo));

        // 减免明细数据
        if (orderInfoVo.getDerateAmount().intValue()!=0){
            OrderDerateVo orderDerateVo = new OrderDerateVo();
            orderDerateVo.setDerateType(SystemConstant.ORDER_DERATE_ALBUM_DISCOUNT);
            orderDerateVo.setDerateAmount(orderInfoVo.getDerateAmount());
            orderDerateVo.setRemarks("专辑购买折扣");
            orderInfoVo.setOrderDerateVoList(Arrays.asList(orderDerateVo));
        }


        // 给OrderInfoVo设置其他的数据(交易编号、付款项目类型、支付方式、时间戳、签名)
        this.setOther(orderInfoVo , tradeVo,redisTemplate);

        return orderInfoVo;
    }

    @Override
    public String getItemType() {
        return SystemConstant.ORDER_ITEM_TYPE_ALBUM;
    }
}